//
//  WFHomePageViewController.m
//  WeiFang
//
//  Created by WeiFang on 2022.
//  Copyright © 2022 WeiFang. All rights reserved.
//

#import "WFHomePageViewController.h"

#import "WFSetUpViewController.h" //设置
#import "WFDaquanRecipesViewController.h" //菜谱大全
#import "WFAddSavingsMethodViewController.h" //添加存钱法
#import "WFWhatEatViewController.h" //一会吃什么
#import "WFSubscribeViewController.h"

#import "WFHomeFunctionCell.h"

@interface WFHomePageViewController ()

@end

@implementation WFHomePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = UIColorFromHex(0xE9EDF9);
    self.leftItem.hidden = YES;
    [self setRightItemWithIcon:[UIImage imageNamed:@"home_set"] selector:@selector(gotoSetUpViewController)];
    [self.view bringSubviewToFront:self.navigationBar];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"WFHomeFunctionCell" bundle:nil] forCellReuseIdentifier:@"WFHomeFunctionCell"];
    
    self.tableView.frame = CGRectMake(0,-self.statusBarHeight, kScreenWidth,kScreenHeight + self.statusBarHeight);
    
    if (!kUserManager.userManager.isSubscribe) {

        if (![[[NSUserDefaults standardUserDefaults] objectForKey:@"isFirstEnter"] boolValue]) {
            WFSubscribeViewController *vc = [WFSubscribeViewController new];
            vc.modalPresentationStyle = UIModalPresentationFullScreen;
            [self.navigationController presentViewController:vc animated:YES completion:nil];
        }
    }
}

#pragma mark - cellDataSource

- (NSArray *)cellDataSource {
    
    if (!self.dataSource) {
        NSMutableArray *arr = [NSMutableArray arrayWithCapacity:4];
        
        __block NSMutableArray *subarr = nil;
        __block NSDictionary *dict = nil;

        subarr = [NSMutableArray arrayWithCapacity:1];
                
        dict =  @{@"class":WFHomeFunctionCell.class,
                  @"height":@([WFHomeFunctionCell getCellFrame:[NSNumber numberWithFloat:kScreenWidth*812/375]]),
                  @"delegate":@YES};
        [subarr addObject:dict];

        [arr addObject:subarr];
        
        self.dataSource = arr;
    }
    return self.dataSource;
}

#pragma mark WFHomeFunctionCellDelegate 菜谱大全 添加存钱法 一会吃什么

- (void)homeFunctionCell:(WFHomeFunctionCell *)cell button:(UIButton *)sender {
    if (!kUserManager.userManager.isSubscribe) {
        [self gotoSubscribeViewController];
        return;
    }
    
    if (sender.tag == 10) {
        //菜谱大全
        [self gotoDaquanRecipesViewController];
    } else if (sender.tag == 11) {
        //添加存钱法
        [self gotoAddSavingsMethodViewController];
    } else if (sender.tag == 12) {
        //一会吃什么
        [self gotoWhatEatViewController];
    }
}

#pragma mark - goto

//设置
- (void)gotoSetUpViewController {
    WFSetUpViewController* vc = [WFSetUpViewController new];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

//菜谱大全
- (void)gotoDaquanRecipesViewController {
    WFDaquanRecipesViewController* vc = [WFDaquanRecipesViewController new];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

//添加存钱法
- (void)gotoAddSavingsMethodViewController {
    WFAddSavingsMethodViewController* vc = [WFAddSavingsMethodViewController new];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

//一会吃什么
- (void)gotoWhatEatViewController {
    WFWhatEatViewController* vc = [WFWhatEatViewController new];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
